﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using UMLTouch.DomainRecognition.Factory;
using UMLTouch.Core.UI;
using System.Collections.ObjectModel;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Ink;
using UMLTouch.Core.ViewModel;
using UMLTouch.Core.Commands;
using System.Windows;
using System.Windows.Data;
using UMLTouch.UI.Start.Views;
using System.Windows.Controls;



namespace UMLTouch.UI.Start.ViewModels
{
    public class ApplicationViewModel : ViewModelBase
    {

        DiagramSelectionViewModel _diagramSelectionViewModel;

        public DiagramSelectionViewModel DiagramSelectionViewModel
        {
            get { return _diagramSelectionViewModel; }
            set
            {
                _diagramSelectionViewModel = value;
                RaisePropertyChanged("DiagramSelectionViewModel");
            }
        }

        DiagramContextViewModelCollection _openedDiagrams;

        Visibility _displayContext;

        public Visibility DisplayContext
        {
            get { return _displayContext; }
            set {
                if (_displayContext == value)
                    return;

                _displayContext = value;
                RaisePropertyChanged("DisplayContext");
            }
        }

        ObservableCollection<PopupViewModel> _popups;

        public ObservableCollection<PopupViewModel> Popups
        {
            get { return _popups; }
            set
            {
                if (_popups == value)
                    return;
                _popups = value;
                RaisePropertyChanged("Popups");
            }
        }

        public DiagramContextViewModelCollection OpenedDiagrams
        {
            get { return _openedDiagrams; }
            set { _openedDiagrams = value; }
        }



        public ApplicationViewModel(String pluginPath)
        {
            DisplayContext = Visibility.Hidden;
            _diagramSelectionViewModel = new DiagramSelectionViewModel(pluginPath);
            _openedDiagrams = new DiagramContextViewModelCollection();
            _openedDiagrams.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_openedDiagrams_CollectionChanged);
            
            InitializePopups();
            InitializeComands();

        }

        void _openedDiagrams_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            DisplayContext = _openedDiagrams.Count > 0 ? Visibility.Visible : Visibility.Hidden;
        }

        private void InitializePopups()
        {
            _popups = new ObservableCollection<PopupViewModel>();
            _diagramSelectionViewModel.PopupValidated += new EventHandler(_diagramSelectionViewModel_Validated);

            _popups.Add(_diagramSelectionViewModel);
        }

        void _diagramSelectionViewModel_Validated(object sender, EventArgs e)
        {
            CreateDiagramContext();
        }



        void CreateDiagramContext()
        {
            var ctxt = _diagramSelectionViewModel.CreateCurrentDiagramContext();

            if (ctxt != null)
            {
                _openedDiagrams.Add(ctxt);
                SetActiveDiagramContext(ctxt);
            }
        }

        void SetActiveDiagramContext(DiagramContextViewModel context)
        {
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.OpenedDiagrams);
            if (collectionView != null)
                collectionView.MoveCurrentTo(context);
        }




        #region Commands

        void InitializeComands()
        {
            NewDiagramCmd = new RelayCommand(cmd =>
            {
                _diagramSelectionViewModel.Show();
            });
        }


        public RelayCommand NewDiagramCmd { get; set; }
        public RelayCommand CreateDiagramCmd { get; set; }
        #endregion
    }
}
